<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Node.js代码实例：简单Web服务端 | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言Node.js代码实例：简单Web服务端。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   为什么要写一份Node.js简单Web服务端的代码实例？ 想多了解一些技术知识，所以简单学习了Node.js 写过C和C++的环境和网络编程，它们的">
<meta property="og:type" content="article">
<meta property="og:title" content="Node.js代码实例：简单Web服务端">
<meta property="og:url" content="http://example.com/2023/04/21/Node.js%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E7%AE%80%E5%8D%95Web%E6%9C%8D%E5%8A%A1%E7%AB%AF/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言Node.js代码实例：简单Web服务端。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   为什么要写一份Node.js简单Web服务端的代码实例？ 想多了解一些技术知识，所以简单学习了Node.js 写过C和C++的环境和网络编程，它们的">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover20.png">
<meta property="article:published_time" content="2023-04-21T05:22:27.000Z">
<meta property="article:modified_time" content="2023-04-26T08:00:23.401Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="Node.js">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover20.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2023/04/21/Node.js%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E7%AE%80%E5%8D%95Web%E6%9C%8D%E5%8A%A1%E7%AB%AF/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Node.js代码实例：简单Web服务端',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-26 16:00:23'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover20.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Node.js代码实例：简单Web服务端</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-21T05:22:27.000Z" title="发表于 2023-04-21 13:22:27">2023-04-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-26T08:00:23.401Z" title="更新于 2023-04-26 16:00:23">2023-04-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Node-js/">Node.js</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Node.js代码实例：简单Web服务端"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>Node.js代码实例：简单Web服务端。</p>
<hr>
<h1 id="代码仓库"><a href="#代码仓库" class="headerlink" title="代码仓库"></a>代码仓库</h1><ul>
<li><a target="_blank" rel="noopener" href="https://github.com/yezhening/Programming-examples">yezhening&#x2F;Programming-examples: 编程实例 (github.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://gitee.com/yezhening/Programming-examples">Programming-examples: 编程实例 (gitee.com)</a></li>
</ul>
<hr>
<h1 id="为什么要写一份Node-js简单Web服务端的代码实例？"><a href="#为什么要写一份Node-js简单Web服务端的代码实例？" class="headerlink" title="为什么要写一份Node.js简单Web服务端的代码实例？"></a>为什么要写一份Node.js简单Web服务端的代码实例？</h1><ul>
<li>想多了解一些技术知识，所以简单学习了Node.js</li>
<li>写过C和C++的环境和网络编程，它们的处理很复杂。了解到Node.js底层使用Libuv网络异步I&#x2F;O库，以事件驱动+异步I&#x2F;O方式运行，适合处理网络I&#x2F;O高并发的场景，和Go语言相似，具有互相比对的学习价值</li>
<li>写过而且写的很多代码都是客户端&#x2F;服务端（C&#x2F;S）模型的网络传输，写一份关于Web服务端的传输，能够深入了解Web服务和HTTP原理</li>
</ul>
<hr>
<h1 id="内容"><a href="#内容" class="headerlink" title="内容"></a>内容</h1><ul>
<li>详细解析创建http服务端的流程、URL的组成、请求消息的结构和响应消息的结构</li>
<li>通过网络传输，浏览器客户端向该Web服务端发送“GET”请求，服务端依据URL响应本地相应的HTML、CSS和JavaScript文件，并在客户端渲染显示页面</li>
</ul>
<hr>
<h1 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h1><p>simple_Web_server目录：</p>
<ul>
<li>server.js文件</li>
<li>www目录</li>
</ul>
<p>www目录：</p>
<ul>
<li>index.html</li>
<li>index.css</li>
<li>index.js</li>
<li>value.html</li>
</ul>
<hr>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><h2 id="server-js"><a href="#server-js" class="headerlink" title="server.js"></a>server.js</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 一、导入http模块</span></span><br><span class="line"><span class="keyword">const</span> http_module = <span class="built_in">require</span>(<span class="string">&#x27;http&#x27;</span>)    <span class="comment">// HTTP相关</span></span><br><span class="line"><span class="keyword">const</span> path_module = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>) <span class="comment">// 字符串路径相关</span></span><br><span class="line"><span class="keyword">const</span> fs_module = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>)    <span class="comment">// 文件系统相关</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 二、创建服务端对象</span></span><br><span class="line"><span class="comment">// 参数：可选项服务端如timeout和keepalive等配置，自动添加到‘request’请求事件的回调函数：当有客户端发起请求时，调用这个回调函数</span></span><br><span class="line"><span class="comment">// 返回值：http.server对象</span></span><br><span class="line"><span class="keyword">const</span> server = http_module.<span class="title function_">createServer</span>()</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> host = <span class="string">&#x27;127.0.0.1&#x27;</span>    <span class="comment">// 主机</span></span><br><span class="line"><span class="keyword">const</span> port = <span class="number">8000</span>   <span class="comment">// 端口</span></span><br><span class="line"><span class="keyword">const</span> request_query_stringing_key = <span class="string">&#x27;key&#x27;</span>   <span class="comment">// 请求url中，查询字符串的键</span></span><br><span class="line"><span class="keyword">const</span> site_root_directory = <span class="string">&#x27;www&#x27;</span>   <span class="comment">// 网站根目录</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 三、服务端对象绑定‘request’请求事件，监听客户端请求</span></span><br><span class="line"><span class="comment">// emitter.on(eventName, listener)是node.js中的事件绑定方法</span></span><br><span class="line"><span class="comment">// 当有客户端发起请求时，调用这个回调函数：传入http.IncomingMessage请求对象和http.ServerResponse响应对象</span></span><br><span class="line">server.<span class="title function_">on</span>(<span class="string">&#x27;request&#x27;</span>, <span class="function">(<span class="params">request, response</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 1. 获取http请求信息</span></span><br><span class="line">    <span class="comment">// http请求有3部分：请求行，请求头和请求体</span></span><br><span class="line">    <span class="comment">// 请求行有3部分：方法，url，http版本</span></span><br><span class="line">    <span class="comment">// （1）获取方法：“GET”，这里不需要</span></span><br><span class="line">    <span class="comment">// const request_method = request.method</span></span><br><span class="line">    <span class="comment">// （1）获取url：“/index.html?key=value”</span></span><br><span class="line">    <span class="keyword">const</span> request_url = request.<span class="property">url</span></span><br><span class="line">    <span class="comment">// （1）获取版本：“1.1”，这里不需要</span></span><br><span class="line">    <span class="comment">// const request_version = request.httpVersion   </span></span><br><span class="line">    <span class="comment">// （2）获取请求头，返回键值对object，这里不需要</span></span><br><span class="line">    <span class="comment">// const request_headers = request.headers </span></span><br><span class="line">    <span class="comment">// （3）获取请求体，返回string。GET方法请求体一般为空，这里不需要</span></span><br><span class="line">    <span class="comment">// let request_body = &#x27;&#x27;</span></span><br><span class="line">    <span class="comment">// request.on(&#x27;data&#x27;, (chunk) =&gt; &#123; // 请求绑定‘data’事件，数据按流传输，有数据时读取chunk为Buffer对象，自动转换为string</span></span><br><span class="line">    <span class="comment">//     request_body += chunk</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">    <span class="comment">// request.on(&#x27;end&#x27;, () =&gt; &#123; // 请求绑定‘end’事件，数据传输完成时触发</span></span><br><span class="line">    <span class="comment">//     console.log(request_body)</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//  2. 处理url。如http://127.0.0.1:8000/index.html?key=value</span></span><br><span class="line">    <span class="comment">// url有4部分：协议“http://”，地址主机名和端口“127.0.0.1:8000”，路径“/index.html”，查询字符串“?key=value”</span></span><br><span class="line">    <span class="comment">// (1）获取协议http，这里不需要</span></span><br><span class="line">    <span class="comment">// (2) 获取地址，这里不需要</span></span><br><span class="line">    <span class="keyword">const</span> absolute_url = <span class="string">&#x27;http://&#x27;</span> + host + <span class="string">&#x27;:&#x27;</span> + port + request_url    <span class="comment">// 获取请求的绝对url。如http://127.0.0.1:8000/index.html?key=value</span></span><br><span class="line">    <span class="keyword">const</span> request_path_query_string = <span class="keyword">new</span> <span class="title function_">URL</span>(absolute_url)  <span class="comment">// 解析绝对url，获取路径和查询字符串。如：/index.html?key=value</span></span><br><span class="line">    <span class="comment">// （3）获取路径。如：/index.html</span></span><br><span class="line">    <span class="keyword">const</span> request_path = request_path_query_string.<span class="property">pathname</span></span><br><span class="line">    <span class="comment">// （4）获取查询字符串。返回object如：&#123;&#x27;key&#x27; =&gt; &#x27;value&#x27;&#125;</span></span><br><span class="line">    <span class="keyword">const</span> request_query_string = request_path_query_string.<span class="property">searchParams</span></span><br><span class="line">    <span class="keyword">const</span> request_query_string_value = request_query_string.<span class="title function_">get</span>(request_query_stringing_key)    <span class="comment">// 获取查询字符串的值。如：通过&#x27;key&#x27;获取&#x27;value&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 结合网络路径和查询字符串，映射为本地文件系统中的路径</span></span><br><span class="line">    <span class="keyword">let</span> fs_path = <span class="string">&quot;&quot;</span></span><br><span class="line">    <span class="keyword">if</span> (request_path === <span class="string">&#x27;/&#x27;</span>) &#123;</span><br><span class="line">        fs_path = path_module.<span class="title function_">join</span>(__dirname, site_root_directory, <span class="string">&quot;index.html&quot;</span>)</span><br><span class="line">        <span class="comment">// 对于url为&#x27;/&#x27;的网络路径，映射为服务端js文件所在当前目录下，网站根目录下的index.html文件路径</span></span><br><span class="line">        <span class="comment">// 如：C:\Users\DSHH\Desktop\simple_Web_server\www\index.html</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (request_path === <span class="string">&#x27;/index.html&#x27;</span> &amp;&amp; request_query_string_value === <span class="string">&#x27;value&#x27;</span>) &#123;</span><br><span class="line">        fs_path = path_module.<span class="title function_">join</span>(__dirname, site_root_directory, <span class="string">&quot;value.html&quot;</span>)</span><br><span class="line">        <span class="comment">// 对于url为&#x27;/index.html&#x27;的网络路径，查询字符串值为&#x27;value&#x27;，映射为服务端js文件所在当前目录下，网站根目录下的value.html文件路径</span></span><br><span class="line">        <span class="comment">// 如：C:\Users\DSHH\Desktop\simple_Web_server\www\value.html</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        fs_path = path_module.<span class="title function_">join</span>(__dirname, site_root_directory, request_path)</span><br><span class="line">        <span class="comment">// 对于其他网络路径，映射为服务端js文件当前目录下，网站根目录下的资源文件路径</span></span><br><span class="line">        <span class="comment">// 如url为：&#x27;/other.html&#x27;，则本地路径为：如：C:\Users\DSHH\Desktop\simple_Web_server\www\other.html</span></span><br><span class="line">        <span class="comment">// 注意：对于其他资源如css、js、ico等请求，全都走这个分支</span></span><br><span class="line">        <span class="comment">// 如：C:\Users\DSHH\Desktop\simple_Web_server\www\index.css、C:\Users\DSHH\Desktop\simple_Web_server\www\favicon.ico</span></span><br><span class="line">        <span class="comment">// 注意：浏览器除了请求路径中的页面，可能会自动请求favicon.ico作为网页的图标，可不管</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 读取本地文件系统相应的文件，设置http响应信息</span></span><br><span class="line">    <span class="comment">// http响应有3部分：响应行，响应头和响应体</span></span><br><span class="line">    <span class="comment">// 响应行有3部分：http版本，状态码，状态描述</span></span><br><span class="line">    <span class="comment">//（1) http版本，无需设置</span></span><br><span class="line">    <span class="comment">// (2) 状态码在下面读取文件时设置</span></span><br><span class="line">    <span class="comment">// (3) 状态描述无需设置，一般与状态码匹配</span></span><br><span class="line">    <span class="comment">// response.statusMessage = &#x27;This is status message&#x27;</span></span><br><span class="line">    <span class="comment">// (4) 响应头</span></span><br><span class="line">    <span class="comment">// 注意：</span></span><br><span class="line">    <span class="comment">// 只需要依据文档类型设置mime类型让浏览器可以解析html、css和js文档</span></span><br><span class="line">    <span class="comment">// 否则当html、css、js分文件编写时，请求html会再次请求css和js文档，再次进入这个回调函数</span></span><br><span class="line">    <span class="comment">// 若mime类型为text/html，则无法解析css文档，css无法渲染</span></span><br><span class="line">    <span class="comment">// 设置编码为utf8支持中文显示不乱码</span></span><br><span class="line">    <span class="keyword">const</span> fs_path_ext_name = path_module.<span class="title function_">extname</span>(fs_path) <span class="comment">// 获取本地路径文件的扩展名，如：.css</span></span><br><span class="line">    <span class="keyword">const</span> fs_path_mime_type = fs_path_ext_name.<span class="title function_">slice</span>(<span class="number">1</span>)   <span class="comment">// 获取mime类型中，text类型下的子类型名，如：css</span></span><br><span class="line">    response.<span class="title function_">setHeader</span>(<span class="string">&#x27;content-type&#x27;</span>, <span class="string">`text/<span class="subst">$&#123;fs_path_mime_type&#125;</span>;charset=utf-8`</span>)   <span class="comment">// 拼接为：text/css;charset=utf-8</span></span><br><span class="line">    <span class="comment">// (5) 响应体，在下面读取文件时设置</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 参数：文件路径和编码等可选项，回调函数</span></span><br><span class="line">    <span class="comment">// 当读取完成时，调用这个回调函数：传入错误Error和数据string对象</span></span><br><span class="line">    fs_module.<span class="title function_">readFile</span>(fs_path, <span class="string">&#x27;utf8&#x27;</span>, <span class="function">(<span class="params">error, data</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 如果有错误，返回404资源不存在状态码和状态信息</span></span><br><span class="line">        <span class="comment">// 没有错误，返回读取的文件数据</span></span><br><span class="line">        <span class="keyword">if</span> (error) &#123;</span><br><span class="line">            response.<span class="property">statusCode</span> = <span class="number">404</span>   <span class="comment">// （2）设置状态码</span></span><br><span class="line">            <span class="comment">// 参数：流数据，可选项编码和回调函数</span></span><br><span class="line">            <span class="comment">// 返回值：bool，不需要</span></span><br><span class="line">            response.<span class="title function_">write</span>(<span class="string">&#x27;&lt;h1&gt;404 未找到&lt;/h1&gt;&#x27;</span>)   <span class="comment">// （5）设置响应体</span></span><br><span class="line">            <span class="comment">// 参数：数据、编码和回调函数等可选项</span></span><br><span class="line">            <span class="comment">// 返回值：this，即end()的调用者response对象</span></span><br><span class="line">            <span class="comment">// 对每个响应可以调用多次write()写入数据，必须调用一次end()返回响应</span></span><br><span class="line">            response.<span class="title function_">end</span>()</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            response.<span class="property">statusCode</span> = <span class="number">200</span>   <span class="comment">// （2）设置状态码</span></span><br><span class="line">            response.<span class="title function_">write</span>(data)    <span class="comment">// （5）设置响应体</span></span><br><span class="line">            response.<span class="title function_">end</span>()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 四、启动服务端监听</span></span><br><span class="line"><span class="comment">// 参数：对于TCP，使用端口、地址、半连接和全连接队列和回调函数可选项</span></span><br><span class="line">server.<span class="title function_">listen</span>(port, host, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`Wen服务端运行在: http://<span class="subst">$&#123;host&#125;</span>:<span class="subst">$&#123;port&#125;</span>`</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="index-html"><a href="#index-html" class="headerlink" title="index.html"></a>index.html</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;./index.css&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>这是index.html<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./index.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="index-css"><a href="#index-css" class="headerlink" title="index.css"></a>index.css</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">h1</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: blue;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="index-js"><a href="#index-js" class="headerlink" title="index.js"></a>index.js</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">alert</span>(<span class="string">&#x27;这是index.js&#x27;</span>)</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="value-html"><a href="#value-html" class="headerlink" title="value.html"></a>value.html</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>这是value.html<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<h1 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h1><p>运行Web服务端：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> C:\Users\DSHH\Desktop\simple_Web_server&gt; node server.js</span><br><span class="line">Wen服务端运行在: http://<span class="number">127.0</span>.<span class="number">0.1</span>:<span class="number">8000</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>浏览器访问： <a target="_blank" rel="noopener" href="http://127.0.0.1:8000/">http://127.0.0.1:8000/</a> 或 <a target="_blank" rel="noopener" href="http://127.0.0.1:8000/index.html">http://127.0.0.1:8000/index.html</a></p>
<p><img src= "" data-lazy-src="/../img/nodejs_web/%E6%88%AA%E5%9B%BE20230421125634.png"></p>
<p><img src= "" data-lazy-src="/../img/nodejs_web/%E6%88%AA%E5%9B%BE20230421125701.png" alt="在这里插入图片描述"></p>
<p>浏览器访问： <a target="_blank" rel="noopener" href="http://127.0.0.1:8000/index.html?key=value">http://127.0.0.1:8000/index.html?key=value</a></p>
<p><img src= "" data-lazy-src="/../img/nodejs_web/%E6%88%AA%E5%9B%BE20230421125803.png" alt="在这里插入图片描述"></p>
<p>浏览器访问： <a target="_blank" rel="noopener" href="http://127.0.0.1:8000/other.html">http://127.0.0.1:8000/other.html</a></p>
<p><img src= "" data-lazy-src="/../img/nodejs_web/%E6%88%AA%E5%9B%BE20230421125848.png" alt="在这里插入图片描述"></p>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>Node.js代码实例：简单Web服务端。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li><a target="_blank" rel="noopener" href="https://nodejs.org/en">Node.js (nodejs.org)</a></li>
<li><a target="_blank" rel="noopener" href="https://nodejs.cn/">Node.js 中文网 (nodejs.cn)</a></li>
<li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1a34y167AZ/?spm_id_from=333.337.search-card.all.click&vd_source=9ac1c0a6d496218112b60d49bc768cd7">黑马程序员Node.js全套入门教程，nodejs新教程含es6模块化+npm+express+webpack+promise等_Nodejs实战案例详解_哔哩哔哩_bilibili</a></li>
<li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1gM411W7ex/?spm_id_from=333.337.search-card.all.click&vd_source=9ac1c0a6d496218112b60d49bc768cd7">尚硅谷2023版Node.js零基础视频教程，nodejs新手到高手_哔哩哔哩_bilibili</a></li>
</ul>
<hr>
<h1 id="作者的话"><a href="#作者的话" class="headerlink" title="作者的话"></a>作者的话</h1><ul>
<li><strong>感谢参考资料的作者&#x2F;博主</strong></li>
<li>作者：夜悊</li>
<li>版权所有，转载请注明出处，谢谢~</li>
<li><strong>如果文章对你有帮助，请点个赞或加个粉丝吧，你的支持就是作者的动力~</strong></li>
<li>文章在描述时有疑惑的地方，请留言，定会一一耐心讨论、解答</li>
<li>文章在认识上有错误的地方, 敬请批评指正</li>
<li>望读者们都能有所收获</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/04/21/Node.js%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E7%AE%80%E5%8D%95Web%E6%9C%8D%E5%8A%A1%E7%AB%AF/">http://example.com/2023/04/21/Node.js代码实例：简单Web服务端/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Node-js/">Node.js</a></div><div class="post_share"><div class="social-share" data-image="/img/cover20.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/04/18/Go%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E7%9A%84%E5%AD%A6%E4%B9%A0%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E7%BD%91%E7%BB%9C%E6%96%87%E4%BB%B6%E4%BC%A0%E8%BE%93/" title="Go网络编程的学习代码示例：网络文件传输"><img class="cover" src= "" data-lazy-src="/img/cover10.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Go网络编程的学习代码示例：网络文件传输</div></div></a></div><div class="next-post pull-right"><a href="/2023/04/24/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E4%BC%A0%E8%BE%93%E6%8E%A7%E5%88%B6%E5%8D%8F%E8%AE%AE%EF%BC%88TCP%EF%BC%89%E7%AE%80%E5%8D%95%E7%89%88/" title="网络编程代码实例：传输控制协议（TCP）简单版"><img class="cover" src= "" data-lazy-src="/img/cover19.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">网络编程代码实例：传输控制协议（TCP）简单版</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%BB%93%E5%BA%93"><span class="toc-number">2.</span> <span class="toc-text">代码仓库</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%86%99%E4%B8%80%E4%BB%BDNode-js%E7%AE%80%E5%8D%95Web%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%9A%84%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9F"><span class="toc-number">3.</span> <span class="toc-text">为什么要写一份Node.js简单Web服务端的代码实例？</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%86%85%E5%AE%B9"><span class="toc-number">4.</span> <span class="toc-text">内容</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84"><span class="toc-number">5.</span> <span class="toc-text">目录结构</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-number">6.</span> <span class="toc-text">代码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#server-js"><span class="toc-number">6.1.</span> <span class="toc-text">server.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#index-html"><span class="toc-number">6.2.</span> <span class="toc-text">index.html</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#index-css"><span class="toc-number">6.3.</span> <span class="toc-text">index.css</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#index-js"><span class="toc-number">6.4.</span> <span class="toc-text">index.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#value-html"><span class="toc-number">6.5.</span> <span class="toc-text">value.html</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BB%93%E6%9E%9C"><span class="toc-number">7.</span> <span class="toc-text">结果</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">8.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">9.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%9A%84%E8%AF%9D"><span class="toc-number">10.</span> <span class="toc-text">作者的话</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover20.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>